package test.socket.obj;

import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.ObjectInputStream;
import java.net.ServerSocket;
import java.net.Socket;

public class Server {
	
	public static void main(String[] args) {
		try {
			@SuppressWarnings("resource")
			ServerSocket ss = new ServerSocket(6666);
			System.out.println("server is listening on port 6666...");
			while(true) {
				Socket s = ss.accept();
				System.out.println("socket " + s.getPort() + " connect...");
				InputStream is = s.getInputStream();
				ObjectInputStream ois = new ObjectInputStream(is);
				String text = ois.readUTF();
				User user1 = (User)ois.readObject();
				User user2 = (User)ois.readObject();
				System.out.println(text);
				System.out.println(user1);
				System.out.println(user2);
				s.close();
			}
		} catch (IOException e) {
			e.printStackTrace();
		} catch (ClassNotFoundException e) {
			e.printStackTrace();
		}
	}
	
	public static String inputStream2String(InputStream is) throws IOException{ 
		ByteArrayOutputStream baos = new ByteArrayOutputStream(); 
		int i=-1; 
		while((i=is.read())!=-1){ 
			baos.write(i); 
		} 
        return baos.toString(); 
	}

}
